<template>
  <div>
    <span class="el-menu__title">查询老人信息</span>
    <el-form style="margin-top: 1%;margin-left: 1%;margin-bottom: 1%">
      <el-row :gutter="10">
        <el-col :span="2" style="text-align: right">
          <span>用户ID:</span>
        </el-col>
        <el-col :span="3">
          <el-input size="mini" placeholder="" v-model="findUser.id" clearable></el-input>
        </el-col>
        <el-col :span="2" style="text-align: left">
          <el-button type="primary" size="mini" @click="query()">查询</el-button>
        </el-col>
        <el-col :span="2" style="text-align: right">
          <span>真实姓名:</span>
        </el-col>
        <el-col :span="3">
          <el-input size="mini" placeholder="" v-model="findUser.username" clearable></el-input>
        </el-col>
        <el-col :span="2" style="text-align: left">
          <el-button type="primary" size="mini" @click="query2()">真实姓名模糊查询</el-button>
        </el-col>
      </el-row>
    </el-form>
    <el-row>

    </el-row>

    <span>查询结果</span>
    <span style="color: red ; font-size: small">(*双击列表老人信息查询老人对应的护工和床位信息))</span>
    <el-row>
      <el-table :data="User" height="200"
                border stripe
                highlight-current-row
                style="width: 100%"
                @current-change="handleCurrentChange"
                @row-dblclick="queryAssis"
                size="mini">
        <el-table-column prop="id" label="编号" align="center" :show-overflow-tooltip="true"
                         sortable></el-table-column>
        <el-table-column prop="username" label="用户名" align="center" :show-overflow-tooltip="true"
                         sortable></el-table-column>
        <el-table-column prop="realname" label="真实姓名" align="center" :show-overflow-tooltip="true"
                         sortable></el-table-column>
        <el-table-column prop="age" label="年龄" align="center" :show-overflow-tooltip="true"
                         sortable></el-table-column>
        <el-table-column prop="sex" label="性别" align="center" :show-overflow-tooltip="true"
                         sortable></el-table-column>
        <el-table-column prop="telephone" label="联系方式" align="center" :show-overflow-tooltip="true"
                         sortable></el-table-column>
        <el-table-column prop="address" label="住址" align="center" :show-overflow-tooltip="true"
                         sortable></el-table-column>
        <el-table-column prop="begTime" label="入住开始时间" align="center" :show-overflow-tooltip="true"
                         sortable></el-table-column>
        <el-table-column prop="endTime" label="入住结束时间" align="center" :show-overflow-tooltip="true"
                         sortable></el-table-column>
        <el-table-column prop="ifee" label="余额" align="center" :show-overflow-tooltip="true"
                         sortable></el-table-column>
        <el-table-column prop="ifeeStatus" label="缴费状态" align="center" :show-overflow-tooltip="true"
                         sortable></el-table-column>
      </el-table>
    </el-row>
    <el-row style="margin-top: 10px">
      <el-col :span="4" style="text-align: right">
        <el-button type="primary" size="mini" @click="dialogVisible = true">修改</el-button>
      </el-col>
      <el-col :span="4" style="text-align: right">
        <el-button type="primary" size="mini" @click="deleteOlder()">删除</el-button>
      </el-col>
    </el-row>
    <span>护工信息</span>
    <el-row style="margin-top: 5px">
      <el-table :data="Assis" height="150"
                border stripe
                highlight-current-row
                style="width: 100%"
                size="mini">
        <el-table-column prop="id" label="编号" align="center" :show-overflow-tooltip="true"
                         sortable></el-table-column>
        <el-table-column prop="username" label="用户名" align="center" :show-overflow-tooltip="true"
                         sortable></el-table-column>
        <el-table-column prop="realname" label="真实姓名" align="center" :show-overflow-tooltip="true"
                         sortable></el-table-column>
        <el-table-column prop="sex" label="性别" align="center" :show-overflow-tooltip="true"
                         sortable></el-table-column>
        <el-table-column prop="age" label="年龄" align="center" :show-overflow-tooltip="true"
                         sortable></el-table-column>
        <el-table-column prop="telephone" label="联系方式" align="center" :show-overflow-tooltip="true"
                         sortable></el-table-column>
      </el-table>
    </el-row>
    <span>床位信息</span>
    <el-row style="margin-top: 5px">
      <el-table :data="bed" height="150"
                border stripe
                highlight-current-row
                style="width: 100%"
                size="mini">
        <el-table-column prop="id" label="编号" align="center" :show-overflow-tooltip="true"
                         sortable></el-table-column>
        <el-table-column prop="level" label="等级" align="center" :show-overflow-tooltip="true"
                         sortable></el-table-column>
        <el-table-column prop="bedStatus" label="状态" align="center" :show-overflow-tooltip="true"
                         sortable></el-table-column>
        <el-table-column prop="room" label="房间" align="center" :show-overflow-tooltip="true"
                         sortable></el-table-column>
        <el-table-column prop="ifee" label="费用" align="center" :show-overflow-tooltip="true"
                         sortable></el-table-column>
      </el-table>
    </el-row>

    <el-dialog
      title="修改老人信息"
      :visible.sync="dialogVisible"
      :before-close="handleClose">

      <el-form :model="Olderform" label-position="right">
        <el-form-item label="编号">
          <el-col :span="4">
            <el-input v-model="Olderform.id" readonly placeholder=""></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="真实姓名">
          <el-col :span="4">
            <el-input v-model="Olderform.realname" readonly placeholder=""></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="年龄">
          <el-col :span="4">
            <el-input v-model="Olderform.age" readonly placeholder=""></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="性别">
          <el-col :span="4">
            <el-input v-model="Olderform.sex" readonly placeholder=""></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="电话" prop="telephone">
          <el-col :span="4">
            <el-input v-model="Olderform.telephone" readonly placeholder=""></el-input>
          </el-col>
          <el-col :span="2" style="text-align: center">
            <span>---------</span>
          </el-col>
          <el-col :span="4">
            <el-input v-model="updateUser.telephone" clearable placeholder="请输入新电话"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="住址" prop="address">
          <el-col :span="4">
            <el-input v-model="Olderform.address" readonly placeholder=""></el-input>
          </el-col>
          <el-col :span="2" style="text-align: center">
            <span>---------</span>
          </el-col>
          <el-col :span="4">
            <el-input v-model="updateUser.address" clearable placeholder="请输入新电话"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="床位编号" prop="bedId">
          <el-col :span="4">
            <el-input v-model="Olderform.bedId" readonly placeholder=""></el-input>
          </el-col>
          <el-col :span="2" style="text-align: center">
            <span>---------</span>
          </el-col>
          <el-col :span="4">
            <el-input v-model="updateUser.bedId" clearable placeholder="请输变更后的床号"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="入住时间" prop="begTime">
          <el-col :span="4">
            <el-input v-model="Olderform.begTime" readonly placeholder=""></el-input>
          </el-col>
          <el-col :span="2" style="text-align: center">
            <span>---------</span>
          </el-col>
          <el-col :span="4">
            <el-input v-model="updateUser.begTime" clearable placeholder="请输入入住时间"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-col :span="4">
            <el-input v-model="Olderform.endTime" readonly placeholder=""></el-input>
          </el-col>
          <el-col :span="2" style="text-align: center">
            <span>---------</span>
          </el-col>
          <el-col :span="4">
            <el-input v-model="updateUser.endTime" clearable placeholder="请输入结束时间"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="护工编号" prop="assId">
          <el-col :span="4">
            <el-input v-model="Olderform.assId" readonly placeholder=""></el-input>
          </el-col>
          <el-col :span="2" style="text-align: center">
            <span>---------</span>
          </el-col>
          <el-col :span="4">
            <el-input v-model="updateUser.assId" clearable placeholder="请输入变更后的护工编号"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item>
          <el-col :span="10" style="text-align: right">
            <el-button type="primary" @click="updateUsers()">修改</el-button>
          </el-col>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
  </span>
    </el-dialog>


  </div>

</template>

<script>
    import {postRequest} from "../../utils/api";

    export default {
        name: "FindOlderInfo",
        data() {
            return {
                findUser: {
                    id: '', // ID
                    username: '', // 用户名
                },
                currentRow: {},
                User: [],
                Assis: [],
                bed: [],
                dialogVisible: false,
                Olderform: {},
                formLabelWidth: '60px',
                updateUser: {
                    telephone: '',
                    address: '',
                    bedId: '',
                    assId: '',
                    begTime: '',
                    endTime: ''
                }
            }
        },
        methods: {

            query() {
                this.User = []
                this.Assis = []
                this.bed = []
                postRequest('/older/findOne',
                    {
                        'id': this.findUser.id === '' ? '0' : this.findUser.id,
                    }
                ).then(res => {
                    // 登录密码校验成功
                    if (res && res.flag === '1') {
                        // 保存登录信息
                        if (res.user.ifeeStatus === '0') {
                            res.user.ifeeStatus = '未缴费'
                        }
                        if (res.user.ifeeStatus === '1') {
                            res.user.ifeeStatus = '已缴费'
                        }
                        this.User.unshift(res.user)
                        this.$message.success(res.msg + '!');
                        // replace 不可回退  铺设可以
                    } else {
                        // 登录失败
                        this.$message.error(res.msg + '!');
                    }
                })

            },
            query2() {
                this.User = []
                postRequest('/older/findByRealnameLike',
                    {
                        'realname': this.findUser.username
                    }
                ).then(res => {
                    // 登录密码校验成功
                    if (res && res.flag === '1') {
                        // 保存登录信息
                        //
                        for (let i = 0, len = res.retList.length; i < len; i++) {
                            if (res.retList[i].ifeeStatus === '0') {
                                res.retList[i].ifeeStatus = '未缴费'
                            }
                            if (res.retList[i].ifeeStatus === '1') {
                                res.retList[i].ifeeStatus = '已缴费'
                            }
                        }
                        for (let i = 0, len = res.retList.length; i < len; i++) {
                            this.User.unshift(res.retList[i])
                        }
                        this.$message.success(res.msg + '!');
                        // replace 不可回退  铺设可以
                    } else {
                        // 登录失败
                        this.$message.error(res.msg + '!');
                    }
                })

            },
            handleCurrentChange(val) {
                this.currentRow = val;
                this.Olderform = val;
            },
            deleteOlder() {
                postRequest('/older/delete',
                    {'id': this.currentRow.id}).then(res => {
                    if (res && res.flag === '1') {
                        this.$message.success(res.msg + '!');
                    } else {
                        // 登录失败
                        this.$message.error(res.msg + '!');
                    }

                })
            },
            queryAssis(val) {
                this.Olderform = val

                postRequest('/assistant/findOne',
                    {
                        'id': val.assId === '' ? '0' : val.assId,
                    }
                ).then(res => {
                    debugger
                    // 登录密码校验成功
                    if (res && res.flag === '1') {
                        if (res.user.assStatus === '0') {
                            res.user.assStatus = '空闲'
                        }
                        if (res.user.assStatus === '1') {
                            res.user.assStatus = '在岗'
                        }
                        if (res.user.assStatus === '2') {
                            res.user.assStatus = '请假'
                        }
                        // 保存信息
                        this.Assis = []
                        this.Assis.push(res.user)
                        this.$message.success(res.msg + '!');
                        // replace 不可回退  铺设可以
                    } else {
                        // 失败
                        this.$message.error(res.msg + '!');
                    }
                })
                postRequest('/bed/findOne',
                    {
                        'id': val.bedId === '' ? '0' : val.bedId,
                    }
                ).then(res => {
                    // 登录密码校验成功
                    if (res && res.flag === '1') {
                        if (res.user.bedStatus === '0') {
                            res.user.bedStatus = '未用'
                        }
                        if (res.user.bedStatus === '1') {
                            res.user.bedStatus = '已用'
                        }
                        if (res.user.bedStatus === '2') {
                            res.user.bedStatus = '损坏'
                        }
                        if (res.user.ifee === 'A') {
                            (res.user.ifee = '50000')
                        }
                        if (res.user.ifee === 'B') {
                            (res.user.ifee = '38000')
                        }
                        if (res.user.ifee === 'C') {
                            (res.user.ifee = '25000')
                        }
                        if (res.user.ifee === 'D') {
                            (res.user.ifee = '15000')
                        }
                        if (res.user.ifee === 'E') {
                            (res.user.ifee = '10000')
                        }
                        if (res.user.ifee === 'F') {
                            (res.user.ifee = '6000')
                        }
                        // 保存信息
                        this.bed = []
                        this.bed.push(res.user)
                        // this.$message.success(res.msg + '!');
                        // replace 不可回退  铺设可以
                    } else {
                        // 失败
                        this.$message.error(res.msg + '!');
                    }
                })

            },
            handleClose(done) {
                this.$confirm('确认关闭？')
                    .then(_ => {
                        done();
                    })
                    .catch(_ => {
                    });
            },
            updateUsers() {
                postRequest(
                    '/older/update',
                    {
                        "id": this.Olderform.id,
                        "realname": this.Olderform.realname,
                        "age": this.Olderform.age,
                        "sex": this.Olderform.sex,
                        "telephone": this.updateUser.telephone === '' ? this.Olderform.telephone : this.updateUser.telephone,
                        "address": this.updateUser.address === '' ? this.Olderform.address : this.updateUser.address,
                        "bedId": this.updateUser.bedId === '' ? this.Olderform.bedId : this.updateUser.bedId,
                        "assId": this.updateUser.assId === '' ? this.Olderform.assId : this.updateUser.assId,
                        "begTime": this.updateUser.begTime === '' ? this.Olderform.begTime : this.updateUser.begTime,
                        "endTime": this.updateUser.endTime === '' ? this.Olderform.endTime : this.updateUser.endTime
                    }
                ).then(res => {
                    // 密码修改校验成功
                    if (res && res.flag === '1') {
                        // 保存登录信息
                        this.$message.success('老人信息修改成功');
                        // replace 不可回退  铺设可以
                        this.$router.push('/Home/FindOlderInfo')
                        this.dialogVisible = false
                    } else {
                        this.loading = false
                        // 修改失败
                        this.$message.error(res.msg + '!');
                    }
                })
            },
        }
    }
</script>

<style scoped>

</style>
